from os import write
from struct import pack,unpack,calcsize
from ResolveWar3 import byte2bit
location = 0

def UnpackData(Byte):
    Byte = Byte
    # location = 0
    def r_unpack(fmt):
        global location
        # nonlocal location
        if type(fmt) == int:
            value = "0x" + Byte[location:location + fmt].hex()
            location += fmt
            return value
        if "x" in fmt:
            unpack(fmt,Byte[location:calcsize(fmt) + location])
            location += calcsize(fmt)
            return
        value = unpack(fmt,Byte[location:calcsize(fmt) + location])[0]
        location += calcsize(fmt)
        if type(value) == bytes:
            value = value.decode()
        return value
    return r_unpack

Attachments = []
PivotPoints = []
Bones = []
# size直接跳了
def main():
    with open('GunGirl.mdx','rb') as fi:
        byte = fi.read()
    CU = UnpackData(byte)
    def UnpackTracksChunk(Tracktype):
        TracksChunk = {
            'tracksCount' : CU('i'),
            'interpolationType' : CU('i'),
            'globalSequenceId' : CU('i'),
        }
        Tracks = []
        for _ in range(TracksChunk['tracksCount']):
            Track = {
                'frame' : CU('i'),
            }
            if Tracktype == 3:
                Track['value'] = [CU('f'),CU('f'),CU('f'),]
                if TracksChunk['interpolationType'] > 1:
                    Track['intan'] = [CU('f'),CU('f'),CU('f'),]
                    Track['outtan'] = [CU('f'),CU('f'),CU('f'),]
            else:
                Track['value'] = [CU('f'),CU('f'),CU('f'),CU('f')]
                if TracksChunk['interpolationType'] > 1:
                    Track['intan'] = [CU('f'),CU('f'),CU('f'),CU('f')]
                    Track['outtan'] = [CU('f'),CU('f'),CU('f'),CU('f')]
            Tracks.append(Track)
        TracksChunk['Tracks'] = Tracks
        return TracksChunk
        # 节点块
    def UnpackNode():
        Node = {
            # 块大小
                'inclusiveSize' : CU('i'),
            # name
                'name' : CU('80s'),
            # 对象id
                'objectId' : CU('i'),
            # 多面体id (用于控制顶点集)
                'parentId' : CU('i'),
            # 标识符
                'flags' : CU('i'),
            }
        r_iocation = location + Node['inclusiveSize'] - calcsize('4i80s')
        while r_iocation > location:
            flag = unpack('4s',byte[location:location+4])[0]
            # 直线
            # 
            if flag == b'KGTR' and CU('4s') == 'KGTR':
                Node['KGTR'] = UnpackTracksChunk(3)
            # 旋转
            # 四元数 
            elif flag == b'KGRT' and CU('4s') == 'KGRT':
                Node['KGRT'] = UnpackTracksChunk(4)
            # 缩放
            # 
            elif flag == b'KGSC' and CU('4s') == 'KGSC':
                Node['KGSC'] = UnpackTracksChunk(3)
        return Node
    if CU('4s') == 'MDLX' :
        while len(byte) > location:
            flag = CU('4s')
            if flag == 'ATCH':
                # 解析BONE结构
                size = CU('l')
                r_iocation = size + location
                while location < r_iocation:
                    Attachment = {
                        'inclusiveSize' : CU('i'),
                        'Node' : UnpackNode()
                    }
                    Attachment['path'] = CU('260s')
                    Attachment['attachmentId'] = CU('i')
                    Attachments.append(Attachment)
            elif flag == 'PIVT' :
                size = CU('i')
                for _ in range(int(size / 12)):
                    point = {
                        'X' : CU('f'),
                        'Y' : CU('f'),
                        'Z' : CU('f'),
                    }
                    PivotPoints.append((point))
            elif flag == 'BONE' :
                size = CU('i')
                r_iocation = size + location
                while location < r_iocation:
                    Bone = {
                        'Node' : UnpackNode(),
                        'geosetId' : CU('i'),
                        'geosetAnimationId' : CU('i'),
                    }
                    Bones.append(Bone)
            else:
                size = CU('l')
                CU("%dx" % (size))
    pass
            
main()
